package router

import (
	"fiberx/internal/global/configs"
	"fiberx/internal/middleware"

	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v3"
	recoverer "github.com/gofiber/fiber/v3/middleware/recover"
)

type structValidator struct {
	validate *validator.Validate
}

// Validate method implementation
func (v *structValidator) Validate(out any) error {
	return v.validate.Struct(out)
}

func Engine(mode string) {
	app := fiber.New(fiber.Config{
		CaseSensitive:   true,
		StrictRouting:   true,
		ServerHeader:    "Fiber",
		StructValidator: &structValidator{validate: validator.New()},
		AppName:         configs.C.Server.Name,
	})

	// 捕获panic、统一响应、日志、cors跨域
	// r.Use(gin.Recovery(), middleware.SystemLogger(), middleware.Cors(), middleware.RateLimitByUser(), middleware.AccessCount())

	// 注册中间件
	// app.Use(logger.New(
	// 	logger.Config{
	// 		Format:     "${time} [${ip}]:${port} ${status} - ${method} ${path} ${latency}\n",
	// 		TimeFormat: "2006-01-02 15:04:05",
	// 		TimeZone:   "Asia/Shanghai",
	// 	},
	// ))
	app.Use(recoverer.New())
	app.Use(middleware.SystemLogger())

	// 注册所有路由(一定要在中间件后面，不然全局中间不会生效)
	InitRouters(app)

	// 启动服务
	gracefulNew(app, configs.C).Run()
}
